﻿
namespace PlaylistCopy.ViewModels.Impl
{
    using PlaylistCopy.Common;
    using PlaylistCopy.Contract;
    using PlaylistCopy.Localization;
    using PlaylistCopy.Logging;
    using PlaylistCopy.Services;
    using PropertyChanged;
    using System;
    using System.Collections.ObjectModel;
    using System.IO;
    using System.Windows.Input;

    [ImplementPropertyChanged]
    public class SummaryWindowVm : ISummaryWindowVm
    {
        private readonly IDialogService dialogService;

        private readonly INotificationService notificationService;

        private readonly IDateTimeProvider dateTimeProvider;

        private readonly IFileService fileService;

        private readonly ILogger logger;

        public SummaryWindowVm(IDialogService dialogService, ILogger logger, INotificationService notificationService,
                               IDateTimeProvider dateTimeProvider, IFileService fileService)
        {
            this.dialogService = dialogService;
            this.logger = logger;
            this.notificationService = notificationService;
            this.dateTimeProvider = dateTimeProvider;
            this.fileService = fileService;

            this.T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public SummaryStatus Status { get; set; }

        public ICommand Close
        {
            get { return new DelegateCommand(() => this.CloseRequest(this, EventArgs.Empty)); }
        }

        public ICommand SaveAs
        {
            get { return new DelegateCommand(this.ExportLog); }
        }

        public ObservableCollection<BufferedLogger.BufferedLoggerEntry> Logs { get; set; }

        public EventHandler CloseRequest { get; set; }

        private void ExportLog()
        {
            var saveFileName = this.dialogService.SaveSingleFile(null, "Text file|*.txt");

            try
            {
                using (var writer = this.fileService.WriteFile(saveFileName, FileMode.Create))
                {
                    writer.WriteLine(this.T("LogFileHeader", this.dateTimeProvider.Now.ToString("D")));
                    writer.WriteLine();

                    foreach (var log in this.Logs)
                    {
                        var logType = log.Type;
                        string logMessage;

                        switch (logType)
                        {
                            case BufferedLogType.Error:
                                logMessage = this.T("Error");
                                break;
                            case BufferedLogType.Info:
                                logMessage = this.T("Info");
                                break;
                            case BufferedLogType.Warning:
                                logMessage = this.T("Warning");
                                break;
                            default:
                                throw new NotSupportedException();
                        }

                        writer.WriteLine("{0}: {1}", logMessage, log.Message);
                    }
                }

                this.notificationService.Info(this.T("LogSavesSuccessfully"), this.T("OperationCompleted"));
            }
            catch (Exception ex)
            {
                this.logger.ErrorException(ex, "Unhandled exception while saving logs.");
                this.notificationService.Error(this.T("UnhandledExceptionWhileSavingLog"));
            }
        }
    }

    public enum SummaryStatus
    {
        Success,
        Error
    }
}
